package com.community.comsys.service.impl;

import java.util.List;

import com.community.common.entity.Building;
import com.community.common.utils.DateUtils;
import com.community.comsys.mapper.*;
import com.community.comsys.service.IBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 楼宇管理Service业务层处理
 * 
 * @author 潇东
 * @date 2025-10-27
 */
@Service
public class BuildingServiceImpl implements IBuildingService
{
    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ParkingSpaceMapper parkingSpaceMapper;

    @Autowired
    private StorageRoomMapper storageRoomMapper;
    /**
     * 查询楼宇管理
     * 
     * @param buildingId 楼宇管理主键
     * @return 楼宇管理
     */
    @Override
    public Building selectBuildingByBuildingId(Long buildingId)
    {
        return buildingMapper.selectBuildingByBuildingId(buildingId);
    }

    /**
     * 查询楼宇管理列表
     * 
     * @param building 楼宇管理
     * @return 楼宇管理
     */
    @Override
    public List<Building> selectBuildingList(Building building)
    {
        return buildingMapper.selectBuildingList(building);
    }

    /**
     * 新增楼宇管理
     * 
     * @param building 楼宇管理
     * @return 结果
     */
    @Override
    public int insertBuilding(Building building)
    {
        building.setCreateTime(DateUtils.getNowDate());
        return buildingMapper.insertBuilding(building);
    }

    /**
     * 修改楼宇管理
     * 
     * @param building 楼宇管理
     * @return 结果
     */
    @Override
    public int updateBuilding(Building building)
    {
        building.setUpdateTime(DateUtils.getNowDate());
        return buildingMapper.updateBuilding(building);
    }

    /**
     * 批量删除楼宇管理
     * 
     * @param buildingIds 需要删除的楼宇管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBuildingByBuildingIds(Long[] buildingIds)
    {
        houseMapper.deleteHouseByHouseIds(buildingIds);
        shopMapper.deleteShopByShopIds(buildingIds);
        parkingSpaceMapper.deleteParkingSpaceByParkingSpaceIds(buildingIds);
        storageRoomMapper.deleteStorageRoomByStorageRoomIds(buildingIds);
        return buildingMapper.deleteBuildingByBuildingIds(buildingIds);
    }

    /**
     * 删除楼宇管理信息
     * 
     * @param buildingId 楼宇管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBuildingByBuildingId(Long buildingId)
    {
        houseMapper.deleteHouseByHouseId(buildingId);
        shopMapper.deleteShopByShopId(buildingId);
        parkingSpaceMapper.deleteParkingSpaceByParkingSpaceId(buildingId);
        storageRoomMapper.deleteStorageRoomByStorageRoomId(buildingId);
        return buildingMapper.deleteBuildingByBuildingId(buildingId);
    }

    /**
     * 导入社区列表
     * @param buildingList
     * @return
     */
    @Override
    public int insertBuildingList(List<Building> buildingList) {
        buildingList.stream().forEach(building -> {
            //为每个楼宇设置创建时间
            building.setCreateTime(DateUtils.getNowDate());
            //调用mapper插入数据
            buildingMapper.insertBuilding(building);
        });
        return buildingList.size();
    }
}
